<?php
class ManagePromotionHotelEditForm extends Form
{
    function ManagePromotionHotelEditForm()
    {
        Form::Form("ManagePromotionHotelEditForm");
        $this->link_css("style/hotel.css");
        $this->link_css('style/jquery-ui-1.9.2.custom.css');
        $this->link_css('style/msgBoxLight.css');
        $this->link_js("javascript/jquery/jquery.ui.core.js");
        $this->link_js("javascript/jquery/jquery.ui.widget.js");
        $this->link_js("javascript/jquery/jquery.ui.mouse.js");
        $this->link_js("javascript/jquery/jquery.ui.sortable.js");
        $this->link_js("javascript/jquery/jquery.ui.datepicker.js");
        //
    }

    function on_submit()
    {
//      var_dump($_POST);die;
        $id = Url::getParamInt('id', 0);
        $promotion['hotel_id'] = Url::getParamInt('hotel_id', 0);
        $promotion['room_id'] = Url::getParamInt('room_id_' . $promotion['hotel_id'], 0);
        $promotion['type'] = Url::getParamInt('type', 0);
//        var_dump($promotion['type']);die;
        $promotion['time_c'] = time();
        $promotion['time_start'] = Url::get('time_start', 0);
        $a = explode("-", $promotion['time_start']);
        $promotion['time_start'] = strtotime($a[1] . "/" . $a[0] . "/" . $a[2]);

        $promotion['time_end'] = Url::get('time_end', 0);
        $b = explode("-", $promotion['time_end']);
        $promotion['time_end'] = strtotime($b[1] . "/" . $b[0] . "/" . $b[2]);

        $promotion['price'] = (int)Url::get('price', 0);
        $promotion['percent'] = (int)Url::get('percent', 0);
        $promotion['status'] = (int)Url::get('status', 0);
        $promotion['user_c'] = User::id();

        $valid = true;
        if ($promotion['price'] == '') {
            $valid = false;
            $this->setFormError("error_name", "Giá khuyến mãi không được để trống!");
        }

        if ((int)Url::get('price', 0) === 1) {
            if ($promotion['percent'] < 1 || $promotion['percent'] > 99) {
                $valid = false;
                $this->setFormError("error_percent", "Phần trăm khuyến mãi nhập không đúng!");
            }
        }
//        var_dump($promotion);die;
        /*if (Db_Promotion::checkpromotion($promotion) != 0) {
            $valid = false;
            $this->setFormError("error_percent", "Trùng thời gian và Loại với khuyến mãi đã có !");
        }*/
        if ($valid) {
            if ($idPromotion = Db_Promotion::updatePromotionHotel($promotion, $id)) { //Thanh cong
                $this->setFormSucces("success", "Save hành công!");
                Url::redirect_current();
            } else {
                $this->setFormError("message", "Có lỗi xảy ra khi thêm vào database.");
            }
        }
    }

    function draw()
    {
        global $display;
        $display->add("WEB_ROOT", WEB_ROOT);
        $display->add("WEB_DIR", WEB_DIR);
        $this->beginForm(true);

        $id = Url::getInt('id', 0);

        if ($id) {
            $hotel_promotion = Db_Promotion::getPromotionHotelById($id);
//            var_dump($hotel_promotion);
            $hotel = Db_Hotel::getHotelById($hotel_promotion["hotel_id"]);
            $room[$hotel['id']] = Db_Room::getRoomsByHotelId($hotel['id']);
//        var_dump($room);
            $display->add("room", $room);
            $hotel_promotion["hotel_name"] = $hotel['name'];
            $hotel_promotion["time_start"] = date("d-m-Y", $hotel_promotion["time_start"]);
            $hotel_promotion["time_end"] = date("d-m-Y", $hotel_promotion["time_end"]);
            $display->add('hotel_promotion', $hotel_promotion);
            $running = Db_Promotion::getPromotionByHotelId($hotel['id']);
    //        var_dump($running);
            $display->add("running", $running);

        }

        $display->add("hotel_cities", Db_Promotion::showDataHotel());
        $display->add("type", Db_Promotion::showDataFilter());

        $display->add("title", "Sửa khuyến mãi khách sạn");
        $display->add("title_submit", "Sửa");
        $display->add("message", $this->showFormErrorMessages(1));
        $display->add("success", $this->showFormSuccesMessages(1));
        $display->output("PromotionHotelForm");
        $this->endForm();
    }
}

?>